package com.pinyou.web.service;

import com.pinyou.item.pojo.*;
import com.pinyou.web.client.BrandClient;
import com.pinyou.web.client.CategoryClient;
import com.pinyou.web.client.GoodsClient;
import com.pinyou.web.client.SpecificationClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class GoodsService {

    @Autowired
    private BrandClient brandClient;

    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private SpecificationClient specificationClient;

    public Map<String,Object> loadData(Long spuId){
        Map<String, Object> map = new HashMap<>();
        // 根据id查询spu对象
        Spu spu = this.goodsClient. querySpuById(spuId);
        // 查询spudetail
         SpuDetail spuDetail = this.goodsClient.querySpuDetailById(spuId);
        // 查询sku集合
        List<Sku> skus = this.goodsClient.querySkuBySpuId(spuId);
        // 查询分类
        List<Long> cids = Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3());
        List<HashMap<String, Object>> categories = cids.stream().map(cid -> {
            String s = this.categoryClient.queryNameByIds(Arrays.asList(cid)).get(0);
            return new HashMap<String, Object>() {{
                put("id", cid);
                put("name", s);
            }};
        }).collect(Collectors.toList());
        // 查询品牌
        Brand brand = this.brandClient.queryBrandById(spu.getBrandId());
        // 查询规格参数组
        List<SpecGroup> groups = this.specificationClient.querySpecsByCid(spu.getCid3());
        // 查询特殊的规格参数
        List<SpecParam> params = this.specificationClient.queryParams(null, spu.getCid3(), null, null);
        Map<Long, String> paramMap = new HashMap<>();
        params.forEach(param -> {
            paramMap.put(param.getId(), param.getName());
        });
        // 封装spu
        map.put("spu", spu);
        // 封装spuDetail
        map.put("spuDetail", spuDetail);
        // 封装sku集合
        map.put("skus", skus);
        // 分类
        map.put("categories", categories);
        // 品牌
        map.put("brand", brand);
        // 规格参数组
        map.put("groups", groups);
        // 查询特殊规格参数
        map.put("paramMap", paramMap);

        return  map;

    }

}